package org.ims.inv.service.project.impl;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.ims.inv.service.project.ServiceProjectConference;

import org.ims.inv.dao.project.DAOProjectConference;
import org.ims.inv.model.flow.confer.ModelFlowConfer;
import org.ims.inv.model.project.ModelProject;
import org.ims.inv.model.project.ModelProjectConference;
import org.ims.inv.model.system.ModelAppUser;

import cn.trymore.core.exception.DAOException;
import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;
import cn.trymore.core.util.UtilString;
import cn.trymore.core.web.paging.PaginationSupport;
import cn.trymore.core.web.paging.PagingBean;

/**
 * Service: 项目会议申请
 * 
 * @author Jeccy.Zhao
 *
 */
public class ServiceProjectConferenceImpl
extends ServiceGenericImpl<ModelProjectConference> implements ServiceProjectConference
{
	
	private DAOProjectConference daoProjectConference;
	
	/**
	 * 
	 * @param dao
	 */
	public ServiceProjectConferenceImpl(DAOProjectConference dao)
	{
		super(dao);
		this.daoProjectConference = dao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.project.ServiceProjectConference#getPaginationByEntity(org.ims.inv.model.project.ModelProject, org.ims.inv.model.project.ModelProjectConference)
	 */
	@Override
	public PaginationSupport<ModelProjectConference> getPaginationByEntity (
			ModelProject project, ModelFlowConfer template, 
			ModelProjectConference conference, PagingBean pagingBean) throws ServiceException
	{
		try
		{
			DetachedCriteria criteria = this.getCriterias(conference);
			DetachedCriteria processCriteria = criteria.createCriteria("process");
			
			if (project != null)
			{
				DetachedCriteria subCriteria = processCriteria.createCriteria("project");
				
				if (project.getProjectManager() != null)
				{
					ModelAppUser projectManager = project.getProjectManager();
					
					if (UtilString.isNotEmpty(projectManager.getUsername()))
					{
						subCriteria.createCriteria("projectManager").add(
								Restrictions.like("username", projectManager.getUsername(), MatchMode.ANYWHERE));
					}
					else if (UtilString.isNotEmpty(projectManager.getId()))
					{
						subCriteria.createCriteria("projectManager").add(
								Restrictions.eq("id", projectManager.getId()));
					}
				}
				
				if (UtilString.isNotEmpty(project.getProjectNo()))
				{
					subCriteria.add(Restrictions.eq("projectNo", project.getProjectNo()));
				}
				
				if (project.getProjectType() != null)
				{
					subCriteria.createCriteria("projectType").add(Restrictions.eq("id", project.getProjectType().getId()));
				}
				
				if (UtilString.isNotEmpty(project.getProjectName()))
				{
					subCriteria.add(Restrictions.like("projectName", project.getProjectName(), MatchMode.ANYWHERE));
				}
			}
			
			if (template != null)
			{
				DetachedCriteria subCriteria = processCriteria.createCriteria("dependedFlow");
				
				if (UtilString.isNotEmpty(template.getId()))
				{
					subCriteria.add(Restrictions.eq("id", template.getId()));
				}
			}
			
			criteria.addOrder(Order.desc("creationTime"));
			
			return this.daoProjectConference.findPageByCriteria(criteria, pagingBean);
		}
		catch (DAOException e)
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelProjectConference domain)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelProjectConference.class);
		
		if (domain != null)
		{
			if (domain.getAuditStatus() != null)
			{
				criteria.add(Restrictions.eq("auditStatus", domain.getAuditStatus()));
			}
			
		}
		else
		{
			criteria.add(Restrictions.isNull("auditStatus"));
		}
		
		return criteria;
	}
	
	public void setDaoProjectConference(DAOProjectConference daoProjectConference)
	{
		this.daoProjectConference = daoProjectConference;
	}

	public DAOProjectConference getDaoProjectConference()
	{
		return daoProjectConference;
	}

}
